home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Preferences.cp
-
- Contains: minimalist preference file routines
-
- Written by:
-
- Copyright: Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 8/19/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
- 11/16/94 DRF Kill a kinda bogus warning by replacing “sizeof(<variable>)”
- with “sizeof(<type>)”.
- 9/27/94 DRF AppLib.h is now Sprocket.h
- 9/9/94 DRF Reordered headers and removed redundant #includes.
-
-
- */
-
- #include "Sprocket.h"
-
- #include <Resources.h>
- #include <TextUtils.h>
- #include <Folders.h>
-
- short
- OpenPreferencesResFile(void)
- {
- OSErr err;
- short prefsVRefNum;
- long prefsDirID;
- HParamBlockRec pb;
- short prefsRefNum;
- Str255 prefsFileName;
-
- err = FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&prefsVRefNum,&prefsDirID);
-
- if (err != noErr) // Couldn’t find preferences folder, something is wrong
- return(-1);
-
- GetIndString(prefsFileName,kPreferencesFileStrings,kPreferencesFileName);
-
- // Try openning the Preferences file
-
- prefsRefNum = HOpenResFile(prefsVRefNum,prefsDirID,prefsFileName,fsRdWrPerm);
- if (prefsRefNum == -1)
- {
- // Get the application’s creator
-
- ProcessInfoRec processInfo;
- ProcessSerialNumber currentProc = {0,kCurrentProcess};
-
- processInfo.processInfoLength = sizeof(ProcessInfoRec);
- processInfo.processName = nil;
- processInfo.processAppSpec = nil;
-
- (void) GetProcessInformation(¤tProc,&processInfo);
-
- // Couldn’t open prefs file, try making a new one
-
- HCreateResFile(prefsVRefNum,prefsDirID,prefsFileName);
- pb.fileParam.ioNamePtr = prefsFileName;
- pb.fileParam.ioVRefNum = prefsVRefNum;
- pb.fileParam.ioDirID = prefsDirID;
- pb.fileParam.ioFDirIndex = 0;
- err = PBHGetFInfo(&pb,false);
-
- pb.fileParam.ioNamePtr = prefsFileName;
- pb.fileParam.ioVRefNum = prefsVRefNum;
- pb.fileParam.ioDirID = prefsDirID;
- pb.fileParam.ioFDirIndex = 0;
- pb.fileParam.ioFlFndrInfo.fdType = 'PREF';
- pb.fileParam.ioFlFndrInfo.fdCreator = processInfo.processSignature;
- pb.fileParam.ioFlFndrInfo.fdFlags = 0;
- err = PBHSetFInfo(&pb,false);
-
- if (ResError() == noErr) // Try opening the newly created prefs file if we made it
- prefsRefNum = HOpenResFile(prefsVRefNum,prefsDirID,prefsFileName,fsRdWrPerm);
- }
-
- return(prefsRefNum);
- }
-